<?php
/* SVN FILE: $Id: app_controller.php 330 2010-07-21 11:52:44Z gregory.rome $ */
/**
 * Application Controller.
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * cBBweb :  digital Blood Bowl league interface (http://code.google.com/p/cbbweb/)
 * Copyright 2009-2010, Grégory Romé.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource    app_controller.php
 * @copyright     Copyright 2009-2010, Grégory Romé.
 * @link          http://code.google.com/p/cbbweb/ cBBweb Project
 * @package       cbbweb
 * @version       $Revision: 330 $
 * @modifiedby    $LastChangedBy: gregory.rome $
 * @lastmodified  $Date: 2010-07-21 11:52:44 +0000 (Wed, 21 Jul 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * Application Controller.
 *
 * @package       cbbweb
 */
class AppController extends Controller {
	var $helpers = array (
		'Menu',
		'Html',
		'Form',
		'Javascript',
		'Number',
		'Paginator',
		'Session',
		'Table'
	);
	var $components = array (
		'RequestHandler',
		'Auth',
		'Acl',
		'Cookie',
		'Session'
	);
	var $locale;
	var $championship;
	var $user;
	var $current;
	var $coach;

	function beforeFilter() {
		parent::beforeFilter();

		$this->locale = Configure :: read('Config.language');

		$this->championship = $this->Session->read('Prefs.Championship');
		if (!$this->championship) {
			if (isset ($this->data['AppController']['Championship'])) {
				$this->championship = $this->data['AppController']['Championship'];
			} else {
				$cookie = $this->Cookie->read('Preferences');
				if(isset($cookie['championship'])) { $this->championship = $cookie['championship']; }
				$this->Session->write('Prefs.Championship', $this->championship);
			}
		}

		$this->coach = $this->Session->read('Prefs.Coach');
		if (!$this->coach) {
			if (isset ($this->data['AppController']['Coach'])) {
				$this->coach = $this->data['AppController']['Coach'];
			} else {
				$cookie = $this->Cookie->read('Preferences');
				if(isset($cookie['coach'])) { $this->coach = $cookie['coach']; }
				$this->Session->write('Prefs.Coach', $this->coach);
			}
		}

		$this->Auth->actionPath = 'controllers/';
		$this->Auth->authorize = 'actions';
		$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
		$this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
		$this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
		$this->Auth->allowedActions = array('display','index', 'view',
			'get_bestplayers', 'get_worstplayers', 'get_bestteams', 'get_worstteams', );

		//$this->Auth->allowedActions = array ('*');
		$this->user['User'] = $this->Session->read('Auth.User');

		if ($this->championship) {
			App :: import('Model', 'Championship');
			$Championship = new Championship;
			$championship = $Championship->find('first', array(
				'conditions' => array('ID' => $this->championship),
				'contain' => array(
					'Team' => array(
					'fields' => array('Team.ID', 'Team.strName'),
					'order' => array('Team.strName COLLATE NOCASE' => 'asc')
			))));
			$this->current['Championship'] = $championship['Championship'];
			if(!isset($championship['Team'])) {
				$championship['Team'] = array();
			}
			$team_list = array();
			$team_ids = array();
			foreach($championship['Team'] as $team)
			{
				$team_list[$team['ID']] = $team['strName'];
				$team_ids[] = $team['ID'];
			}

			$this->current['Team'] = $team_list;
			$this->current['Teams'] = $team_ids;
		}

		if ($this->coach) {
			App :: import('Model', 'Coach');
			$Coach = new Coach;
			$coach = $Coach->find('first', array(
				'conditions' => array('ID' => $this->coach),
				'contain' => array(
					'Team' => array(
						'fields' => array('Team.ID', 'Team.strName'),
						'order' => array('Team.strName COLLATE NOCASE' => 'asc'),
						'Player'=> array(
							'fields' => array('Player.ID', 'Player.strName')
			)))));

			$team_list = array();
			$team_ids = array();
			foreach($coach['Team'] as $team)
			{
				$team_list[$team['ID']] = $team['strName'];
				$team_ids[] = $team['ID'];

				$player_list = array();
				$player_ids = array();
				foreach($team['Player'] as $player)
				{
					$player_list[$player['ID']] = $player['strName'];
					$player_ids[] = $player['ID'];
				}
			}

			$this->current['Coach'] = $coach['Coach'];
			$this->current['Coach']['Team'] = $team_list;
			$this->current['Coach']['Teams'] = $team_ids;
			$this->current['Coach']['Players'] = $player_list;
			$this->current['Coach']['Players'] = $player_ids;
		}
	}

	function beforeRender() {
		parent::beforeFilter();

		if (isset ($this->data['AppController']['Championship'])) {
			$this->championship = $this->data['AppController']['Championship'];

			$cookie = array ();
			$cookie['championship'] = $this->data['AppController']['Championship'];
			$this->Cookie->write('Preferences', $cookie, true, '+2 weeks');
			$this->Session->write('Prefs.Championship', $this->data['AppController']['Championship']);
		}

			if (isset ($this->data['AppController']['Coach'])) {
			$this->coach = $this->data['AppController']['Coach'];

			$cookie = array ();
			$cookie['coach'] = $this->data['AppController']['Coach'];
			$this->Cookie->write('Preferences', $cookie, true, '+2 weeks');
			$this->Session->write('Prefs.Coach', $this->data['AppController']['Coach']);
		}

		$this->set('locale', $this->locale);

		$user = $this->Session->read('Auth.User');
		if ($user) {
			$this->set('user', $user['username']);
		}
		Configure :: load('options');
		$this->set('ganalytics', Configure :: read('GoogleAnalytics.code'));

		/* Define current championship -------------------------------------- */
		App :: import('Model', 'Championship');
		$Championship = new Championship;
		$championships_list = $Championship->find('list', array (
			'fields' => array ('Championship.ID','Championship.strName'),
			'order' => array('Championship.strName' => 'asc')
		));
		$this->set('championships_list', $championships_list);
		$prefs = $this->Session->read('Prefs');
		$this->set('current_championship', $prefs['Championship']);

		/* Define current coach -------------------------------------- */
		App :: import('Model', 'Coach');
		$Coach = new Coach;
		$coaches_list = $Coach->find('list', array (
			'fields' => array ('Coach.ID','Coach.strName'	),
			'order' => array('Coach.strName' => 'asc')
		));
		$this->set('coaches_list', $coaches_list);
		$prefs = $this->Session->read('Prefs');
		$this->set('current_coach', $prefs['Coach']);

		$this->set('coach_id', $this->coach);
	}

	function changeDbSource($database = 'default') {
		$db = ConnectionManager :: getInstance();
		$connected = $db->getDataSource($database);
		if ($connected->isConnected()) {
			return true;
		}
		return false;
	}

	function debug($data,$exit=true) {
		echo '<pre>';
		print_r($data);
		echo '<pre>';
		if($exit) exit(0);
	}

  function isUploadedFile($file) {
    if ((isset($file['error']) && $file['error'] == 0) ||
    (!empty($file['tmp_name']) && $file['tmp_name'] != 'none'))
    {
      return is_uploaded_file($file['tmp_name']);
  	} else {
      return false;
    }
  }
}
?>
